/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vadung.contragame2d;

import javax.microedition.lcdui.Graphics;

/**
 * Enemy 2 : Not move, direction fire bullet to player
 *
 * @author AnhDung~
 */
//
public class MyEnemyFixed extends MyActor {

    private int m_iMaxBullet; // Max bullet shot once time
    private int m_iDesX; // Destination X : Fire bullet fly to the location x.
    private int m_iDesY; // Destination Y : Fire bullet fly to the location y.
    private long m_timeFire;

    public void SetDestinationForBullet(int posX, int posY) {
        m_iDesX = posX;
        m_iDesY = posY;
    }

    public MyEnemyFixed(int[] aiSpriteID) {
        super(aiSpriteID);
        Init();
    }

    public MyEnemyFixed(String[] astrSpriteName) {
        super(astrSpriteName);
        Init();
    }

    private void Init() {
        m_isDie = false;
        m_iMaxBullet = 3;
        m_timeFire = System.currentTimeMillis();
    }

    public void Update() {
        if (m_isDie) {
            return;
        }

        super.Update(); //To change body of generated methods, choose Tools | Templates.

        MySprite sprEnemy = this.GetCurrentSpriteAction();
        GameLayerManager layerManager = GameLayerManager.GetInstance();

        if (m_iDesX < m_iXMap) {
            if (m_iDesY > m_iYMap - 20 && m_iDesY < m_iYMap + m_iHeight + 20) {
                sprEnemy.SetCurrentFrame(0);
                SetState(DEFINE.ACTOR_STATE_MOVE_LEFT);

            } else if (m_iDesY < m_iYMap && m_iDesX > m_iXMap - 20 && m_iDesX < m_iXMap + m_iWidth + 20) {
                sprEnemy.SetCurrentFrame(0);
                SetState(DEFINE.ACTOR_STATE_UP);

            } else if (m_iDesY < m_iYMap) {
                sprEnemy.SetCurrentFrame(1);
                SetState(DEFINE.ACTOR_STATE_MOVE_LEFT_UP);

            } else {
                sprEnemy.SetCurrentFrame(3);
                SetState(DEFINE.ACTOR_STATE_MOVE_LEFT_DOWN);
            }
        } else {
            if (m_iDesY > m_iYMap - 20 && m_iDesY < m_iYMap + m_iHeight + 20) {
                sprEnemy.SetCurrentFrame(6);
                SetState(DEFINE.ACTOR_STATE_MOVE_RIGHT);

            } else if (m_iDesY > m_iYMap && m_iDesX > m_iXMap - 20 && m_iDesX < m_iXMap + m_iWidth + 20) {
                sprEnemy.SetCurrentFrame(5);
                SetState(DEFINE.ACTOR_STATE_DOWN);

            } else if (m_iDesY < m_iYMap) {
                sprEnemy.SetCurrentFrame(7);
                SetState(DEFINE.ACTOR_STATE_MOVE_RIGHT_UP);
            } else {
                sprEnemy.SetCurrentFrame(4);
                SetState(DEFINE.ACTOR_STATE_MOVE_RIGHT_DOWN);
            }
        }


        if (m_iMaxBullet > MyObject.GetInstance().GetSumBullet(this) && layerManager.IsOutWindowView(m_iXMap-10, m_iYMap, m_iWidth, m_iHeight) == false) {

            if (System.currentTimeMillis() - m_timeFire > 1000) {
                int aiSpriteID[] = new int[]{2};
                MyBullet bullet = new MyBullet(this, aiSpriteID);
                bullet.SetPositionOnMap(m_iXMap + m_iWidth / 2, m_iYMap + m_iHeight / 2);
                // bullet.SetFlyDelta(tX, tY);
                bullet.m_state = this.m_state;
                MyObject.GetInstance().Add(bullet);
                m_timeFire = System.currentTimeMillis();
            }
        }
    }

    public void Render(Graphics g) {
        super.Render(g); //To change body of generated methods, choose Tools | Templates.
    }
}
